<?php
/**
 * 创建人 : Forpastime
 * 创建时间: 2023/3/13 14:18
 * 个人主页 : http://forpastime.com
 */

namespace App\Utils;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Utils\ApplicationContext;

class Result
{
    /**
     * @Inject()
     * @var RequestInterface
     */
    public static $request;


    /**
     * @Inject()
     * @var ResponseInterface
     */
    public static $response;


    /**
     * Restful 操作成功 JSON格式返回
     * @param mixed $msg 提示信息
     * @param mixed $data 返回的数据
     * @param array $header 发送的Header信息
     */
    public static function success($data = [], $msg = '请求成功', array $header = [])
    {
        $code = 1;
        $result = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
        $response = ApplicationContext::getContainer()->get(ResponseInterface::class);

        return $response->withHeader('Access-Control-Allow-Origin', '*')
            ->withHeader('Access-Control-Allow-Credentials', 'true')->withHeader('Access-Control-Allow-Headers','*')->withHeader('Access-Control-Allow-Methods','GET,PUT,POST,OPTIONS')->withAddedHeader('content-type', 'application/json; charset=utf-8')
            ->withBody(new SwooleStream(json_encode($result,JSON_UNESCAPED_UNICODE)));
    }

    /**
     * Restful 操作错误 JSON格式返回.
     * @param mixed $msg 提示信息,若要指定错误码,可以传数组,格式为['code'=>您的错误码,'msg'=>'您的错误消息']
     * @param mixed $data 返回的数据
     * @param array $header 发送的Header信息
     */
    public static function error($msg = '', $data = [], array $header = [])
    {
        $code = 0;
        if (is_array($msg)) {
            $code = $msg['code'];
            $msg = $msg['msg'];
        }
        $result = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
        $response = ApplicationContext::getContainer()->get(ResponseInterface::class);
        return $response->withHeader('Access-Control-Allow-Origin', '*')
            ->withHeader('Access-Control-Allow-Credentials', 'true')->withHeader('Access-Control-Allow-Headers','*')->withHeader('Access-Control-Allow-Methods','GET,PUT,POST,OPTIONS')->withAddedHeader('content-type', 'application/json; charset=utf-8')
            ->withBody(new SwooleStream(json_encode($result,JSON_UNESCAPED_UNICODE)));
    }


    /**
     * 结果返回
     * @param mixed $code 提示信息
     * @param mixed $data 返回的数据
     */
    public function result($data = [],$code = 1)
    {
        $result = [
            'code' => $code,
            'data' => $data,
        ];
        return self::$response->withHeader('Access-Control-Allow-Origin', '*')
            ->withHeader('Access-Control-Allow-Credentials', 'true')->withHeader('Access-Control-Allow-Headers','*')->withHeader('Access-Control-Allow-Methods','GET,PUT,POST,OPTIONS')->withAddedHeader('content-type', 'application/json; charset=utf-8')->json($result);
    }

    /**
     * 自定义返回数据
     * @param array $data
     * @param int $code
     * @param string $msg
     * @return array
     */
    public static function return($data=[],$code=1,$msg="")
    {
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
    }

    /**
     * 返回成功数据
     * @param array $data
     * @return array
     */
    public static function data_success($data=[])
    {
        return [
            'code' => 1,
            'data' => $data,
        ];
    }

    /**
     * 返回错误提示
     * @param string $msg
     * @param array $data
     * @return array
     */
    public static function data_error($msg='',$data=[])
    {
        return [
            'code' => 0,
            'msg' => $msg,
            'data' => $data,
        ];
    }
}